package uk.ac.open.kmi.dyniqx.wrappers;

import java.util.Comparator;

public class TermWrapper {
	private String word;
	private int freq;
	
	public TermWrapper() {
		this.word = "";
		this.freq = 0;
	}

	public TermWrapper(String word, int freq) {
		this.word = word;
		this.freq = freq;
	}

	public String getWord() {
		return word;
	}

	public void setWord(String word) {
		this.word = word;
	}

	public int getFreq() {
		return freq;
	}

	public void setFreq(int freq) {
		this.freq = freq;
	}
	
	public int compareTo(Object anotherTermWrapper) throws ClassCastException {
		if (!(anotherTermWrapper instanceof TermWrapper))
		    throw new ClassCastException("A TermWrapper object expected.");
		int anotherFreq = ((TermWrapper) anotherTermWrapper).getFreq();  
		return this.freq - anotherFreq;    
	}

	@SuppressWarnings("unchecked")
	public static Comparator WordComparator = new Comparator() {
		public int compare(Object TermWrapper, Object anotherTermWrapper) {
		    String word1 = ((TermWrapper) TermWrapper).getWord();
		    Integer freq1 = ((TermWrapper) TermWrapper).getFreq();
		    String word2 = ((TermWrapper) anotherTermWrapper).getWord();
		    Integer freq2 = ((TermWrapper) anotherTermWrapper).getFreq();

		    if (!(word1.equals(word2)))
		        return word1.compareTo(word2);
		    else
		        return freq1.compareTo(freq2);
		}
	};

	@SuppressWarnings("unchecked")
	public static Comparator FreqComparator = new Comparator() {
		public int compare(Object TermWrapper, Object anotherTermWrapper) {
		    String word1 = ((TermWrapper) TermWrapper).getWord();
		    Integer freq1 = ((TermWrapper) TermWrapper).getFreq();
		    String word2 = ((TermWrapper) anotherTermWrapper).getWord();
		    Integer freq2 = ((TermWrapper) anotherTermWrapper).getFreq();

		    if (!(freq1.equals(freq2)))
		        return freq1.compareTo(freq2);
		    else
		        return word1.compareTo(word2);
		}
	};

}
